<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width='550px'  :fullscreen = 'Global.currentDevice'>
      <el-form   label-width="80px" ref='form' :model='form'  class='mr30  mt28'>
              <el-form-item      label="名称" prop="name" align='left' :rules='{required:true,message:"请输入名称"}'>
                    <el-input v-model.trim="form.name"   placeholder='请输入名称'></el-input>
              </el-form-item>
              
      </el-form>
      <div slot="footer" class="dialog-footer">
              <el-button @click="close()">取 消</el-button>
              <el-button type="primary" @click="submit('form')">确 定</el-button>
      </div>
  </el-dialog>
</template>

<script charset="utf-8" type="text/javascript">

export default {
  name: 'tableName',
  data() {

    return {
      loading: false,
      //dialogo基本信息  显示隐藏状态  title 
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      search:{
        name: '',
        limit: 10,
        currentPage:1,
      },
      form:{
        name:'',
        code:'',
        type:''
      },
      isEdit:false,
      isFile:false,
      file_last_part:'',
    }
  },
  
  methods: {
    
    submit(formName) {
      this.loading = true
      //先验证表达
      this.$refs[formName].validate(valid => {
          //通过验证后
          if (valid) {
              if(this.isEdit == false){
                  this.$emit('save-finished',this.form,false)
              }
              else{
                  if(this.isFile == true){
                      this.form.name+=this.file_last_part
                  }
                  this.$emit('save-finished',this.form,true,this.form.type)
                  
              }
              this.dialogProps.visible=false
          }
      })
    },
    close(){
      this.dialogProps.visible = false
      this.resetForm('form')
      this.clearValidateForm('form')
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('openDialog', function() {
        this.dialogProps.visible = true
        this.dialogProps.title = '新建文件夹'
        this.isEdit = false
        this.resetForm('form')
        this.clearValidateForm('form')
        this.form.name = ''
        
      })
      this.$on('openEditDialog',function(name,code,type){
        this.dialogProps.visible = true
        this.isEdit =true

        this.resetForm('form')
        this.clearValidateForm('form')

        this.form.code=code
        
        this.form.type=type
        if(type == 'folder'){
            this.isFile = false
            this.dialogProps.title = '文件夹重命名'
            this.form.name=name
        }
        if(type == 'file'){
            this.isFile = true
            this.dialogProps.title = '文件重命名'
            // this.form.name=name.split('.')[0]
            //从最后一个字符开始匹配.
            let last_index = name.lastIndexOf('.')
            let fir_part = name.slice(0,last_index)
            let sec_part = name.slice(last_index)
            this.form.name = fir_part
            this.file_last_part = sec_part
        }
      })
      
    })
  },
  updated(){
    
  },
  components:{
    
  }
}
</script>

<style  scoped>
</style>


